package cn.edu.zsc.blms;

import cn.edu.zsc.blms.service.BookService;
import cn.edu.zsc.blms.service.BorrowingService;
import cn.edu.zsc.blms.service.ReaderService;
import cn.edu.zsc.blms.service.impl.array.BookServiceImpl;
import cn.edu.zsc.blms.service.impl.array.ReaderServiceImpl;
import cn.edu.zsc.blms.service.impl.array.collection.*;

import java.util.Scanner;

public class App {
    public static Scanner input;

    public final ReaderService readerService;
    public final BookService bookService;
    public final BorrowingService borrowingService;


    public void run() {
        input = new Scanner(System.in);
        String systemMenuChoice;
        do {
            systemMenu();
            systemMenuChoice = input.nextLine();
            switch (systemMenuChoice) {
                case "0":
                    System.out.println("成功退出系统，欢迎再次使用！");
                    break;
                case"1":
                    String readerMenuChoice;
                    do{
                        readerMenu();
                        readerMenuChoice =input.nextLine();
                        switch(readerMenuChoice){
                            case"0":
                                break;
                            case"1":
                                readerService.create();
                                break;
                            case"2":
                                readerService.disable();
                                break;
                            case"3":
                                readerService.update();
                                break;
                            case"4":
                                readerService.query();
                                break;
                            default:
                                System.out.println("输入非法，请输入正确的菜单编号！");
                        }
                    }while(!("0").equals(readerMenuChoice));

                    break;
                case"2":
                    do{
                        bookMenu();
                        readerMenuChoice=input.nextLine();
                        switch(readerMenuChoice){
                            case"0":
                                break;
                            case"1":
                                bookService.create();
                                break;
                            case"2":
                                bookService.disable();
                                break;
                            case"3":
                                bookService.update();
                                break;
                            case"4":
                                bookService.query();
                                break;
                            default:
                                System.out.println("输入非法，请输入正确的菜单编号！");
                        }
                    }while(!("0").equals(readerMenuChoice));
                    break;
                case"3":
                    do{
                        borrowingMenu();
                        readerMenuChoice=input.nextLine();
                        switch(readerMenuChoice){
                            case"0":
                                break;
                            case"1":
                                borrowingService.borrow();
                                break;
                            case"2":
                                borrowingService.giveBack();
                                break;
                            case"3":
                               borrowingService.query();
                                break;
                            default:
                                System.out.println("输入非法，请输入正确的菜单编号！");
                        }
                    }while(!("0").equals(readerMenuChoice));
                    break;
                default:
                    System.out.println("输入非法，请输入正确的菜单编号！");
            }
        }while(!"0".equals(systemMenuChoice));
        input.close();
    }



    public App(ReaderService readerService, BookService bookService,BorrowingService borrowingService){
        this.readerService=readerService;
        this.bookService = bookService;
        this.borrowingService = borrowingService;
    }
    public static void main(String[] args) {
        App app = new App(ReaderServiceImpl.getSingleton(),BookServiceImpl.getSingleton(), BorrowingServiceImpl.getSingleton());
        app.run();
    }
    public void systemMenu() {
        System.out.println("-------------------");
        System.out.println("    图书管理系统   ");
        System.out.println("-------------------");
        System.out.println("|   0...退出系统     |");
        System.out.println("|   1...读者管理     |");
        System.out.println("|   2...图书管理     |");
        System.out.println("|   3...借还图书     |");
        System.out.println("-------------------");
        System.out.println("请输入选项：");
    }
    public void readerMenu(){
        System.out.println("-------------------");
        System.out.println("     读者管理     ");
        System.out.println("-------------------");
        System.out.println("|   0...返回上级     |");
        System.out.println("|   1...添加读者     |");
        System.out.println("|   2...禁用读者     |");
        System.out.println("|   3...编辑读者     |");
        System.out.println("|   4...查询读者     |");
        System.out.println("-------------------");
        System.out.println("请输入选项：");
    }

    public void bookMenu(){
        System.out.println("-------------------");
        System.out.println("       图书管理      ");
        System.out.println("-------------------");
        System.out.println("|   0...返回上级     |");
        System.out.println("|   1...添加图书     |");
        System.out.println("|   2...废弃图书     |");
        System.out.println("|   3...编辑图书     |");
        System.out.println("|   4...查询图书     |");
        System.out.println("-------------------");
        System.out.println("请输入选项：");
    }

    public void borrowingMenu(){
        System.out.println("-------------------");
        System.out.println("       借还图书      ");
        System.out.println("-------------------");
        System.out.println("|   0...返回上级     |");
        System.out.println("|   1...借阅图书     |");
        System.out.println("|   2...归还图书     |");
        System.out.println("|   3...查询记录     |");
        System.out.println("-------------------");
        System.out.println("请输入选项：");
    }
}